package com.itheima.webserver.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.commons.model.domain.Admin;
import com.itheima.dubbo.interfaces.PublishApi;
import com.itheima.webserver.config.NoAuthorization;
import com.itheima.webserver.interceptor.AdminThreadLocal;
import com.itheima.webserver.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: alongchan
 * @Date: 2021/12/16 19:33
 * @Description: 系统框架
 */
@RestController
@RequestMapping("/system")
public class SystemController {
    @Autowired
    private SystemService systemService;

    //登录获取验证码
    @NoAuthorization
    @GetMapping("/users/verification")
    public ResponseEntity getVerification(@RequestParam("uuid") String UUID, HttpServletResponse response) throws IOException {
        systemService.getVerification(UUID, response);
        return ResponseEntity.ok(null);
    }

    //后台管理员登录
    @NoAuthorization
    @PostMapping("/users/login")
    public ResponseEntity adminLogin(@RequestBody Map<String, String> params) {








        String token = systemService.adminLogin(params);
        Map<String, String> map = new HashMap<>();
        map.put("token", token);
        return ResponseEntity.ok(map);
    }

    //管理员基本信息
    @PostMapping("/users/profile")
    public ResponseEntity adminProfile() {
        Admin admin = systemService.getAdminProfile(AdminThreadLocal.getUserId());
        return ResponseEntity.ok(admin);
    }

    //管理员登出
    @PostMapping("/users/logout")
    public ResponseEntity adminLoginOut() {
        systemService.adminLoginOut();
        return ResponseEntity.ok(null);

    }
}
